// pages/Visitors/Visitors.js
import {
    findTravelerInfos,
    deleteById
} from "../../api/home"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        yonghuList: '',
        isflge: 1,
    },
    //跳转添加参观者页面
    buybtn1() {
        wx.navigateTo({
            url: '../add_commit/add_commit',
        })
    },
    onClickLeft() {
        wx.navigateBack({
            delta: 1,
        })
    },
    //点击用户提醒黑名单
    inBlacklist(e){
    //    console.log(e.currentTarget.dataset.id);
       const index  = e.currentTarget.dataset.id
    //    console.log(this.data.yonghuList[index]);
       let _this = this
       if(this.data.yonghuList[index].inBlacklist){
           console.log(this.data.yonghuList[index].name);
        wx.showModal({
            title:"温馨提示",
            content: '用户:' + _this.data.yonghuList[index].name + '被拉入黑名单,原因是“' +_this.data.yonghuList[index].reason + "”" ,
        })
       }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        findTravelerInfos().then(res => {
            console.log(res);
            if (res.code == 1000) {
                this.setData({
                    yonghuList: res.data
                })
            }
        })
        // this.setData({
        //     yonghuList: wx.getStorageSync('personInfoArr')
        // })
        console.log(this.data.yonghuList);
    },
    //点击编辑
    bianji(e) {
        console.log(e);
        const index = e.currentTarget.dataset.id
        // this.data.yonghuArrs[index]
        console.log(this.data.yonghuList[index]);
        let id = this.data.yonghuList[index].id
        let username = this.data.yonghuList[index].name;
        let cardNumber = this.data.yonghuList[index].cardNum;
        let certType = this.data.yonghuList[index].certType;
        let faceUrl = this.data.yonghuList[index].faceUrl;
        let phone = this.data.yonghuList[index].phone;
        let flge = 1
        wx.navigateTo({
            url: '/pages/addcommits/addcommits?id=' + id + '&username=' + username + '&cardNumber=' + cardNumber + '&certType=' + certType + '&faceUrl=' + faceUrl + '&phone=' + phone + '&isflge=' + this.data.isflge +"&flge=" +flge ,
        })
    },
    //删除
    deletap(e) {
        // console.log(e);
        const index = e.currentTarget.dataset.index;
        // console.log(this.data.yonghuList);
        
        // console.log(this.data.yonghuList[index]);
        // console.log(this.data.yonghuList[index]);
        let _this = this
        wx.showModal({
            title: '确定删除参观者' + _this.data.yonghuList[index].name + "?",
            confirmColor: '#0052D9 ',
            success(res) {
                if (res.confirm) {
                    deleteById({
                        id:_this.data.yonghuList[index].id
                    }).then(res=>{
                        console.log(res);
                        if(res.code == 1000){
                            wx.showLoading({
                              title: '加载中',
                            })
                            findTravelerInfos().then(res => {
                                console.log(res);
                                if (res.code == 1000) {
                                    _this.setData({
                                        yonghuList: res.data
                                    })
                                    wx.hideLoading({
                                      success: (res) => {},
                                    })
                                }
                            })
                        }else{
                            wx.showModal({
                                title:"温馨提示",
                              cancelColor: res.data.code,
                            })
                        }
                    })
                    console.log(_this.data.yonghuList[index].username);
                    // _this.data.yonghuList.splice(index, 1)
                    // _this.setData({
                    //     yonghuList: _this.data.yonghuList
                    // })
                    // wx.setStorageSync('personInfoArr', _this.data.yonghuList)
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        findTravelerInfos().then(res => {
            console.log(res);
            if (res.code == 1000) {
                this.setData({
                    yonghuList: res.data
                })
            }
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})